package com.micosco.taskremind.ui;

import android.content.Intent;
import android.os.Bundle;

import com.micosco.taskremind.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.micosco.taskremind.data.TaskDatabase;
import com.micosco.taskremind.data.TaskList;
import com.micosco.taskremind.ui.handler.HighlightTaskRefreshHandler;
import com.micosco.taskremind.ui.handler.RefreshHandler;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;

import java.util.concurrent.atomic.AtomicBoolean;

//TODO 在appbar显示最近截止作业
public class MainActivity extends AppCompatActivity {
    private RefreshHandler highlightRefreshHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (TaskDatabase.getInstance() == null) {
            TaskDatabase.createInstance(this);
        }
        TaskDatabase db = TaskDatabase.getInstance();

        setContentView(R.layout.activity_scrolling);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> {
            Intent intent = new Intent(getApplicationContext(), TaskInfoActivity.class);
            intent.putExtra("TYPE", TaskInfoActivity.NEW_TASK);
            startActivity(intent);
        });

        Button detail = findViewById(R.id.show_detail_buttom);
        detail.setOnClickListener((view) -> {
            Intent intent = new Intent(getApplicationContext(), TaskInfoActivity.class);
            intent.putExtra("TYPE", TaskInfoActivity.TASK_INFO);
            intent.putExtra("UID", TaskList.getInstance().getEarliestDueTask().getUID());
            startActivity(intent);
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        highlightRefreshHandler = new HighlightTaskRefreshHandler(this);
        highlightRefreshHandler.startRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();

        highlightRefreshHandler.startRefresh();
    }

    @Override
    protected void onPause() {
        super.onPause();

        highlightRefreshHandler.startRefresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        highlightRefreshHandler.stopRefresh();
    }

}